{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "获取诗歌任务启动\n",
      "朗读诗歌任务启动\n",
      "发送内容\n",
      "接收内容\n",
      "发送内容\n",
      "接收内容\n",
      "发送内容\n",
      "接收内容\n",
      "发送内容\n",
      "接收内容\n",
      "发送内容\n",
      "接收内容\n"
     ]
    }
   ],
   "source": [
    "#　导入所需的模块\n",
    "from multiprocessing import Process, Queue\n",
    "import os\n",
    "import time\n",
    "\n",
    "#李白诗集测试列表的迭代\n",
    "rd_poem = [\"李白诗集\",\\\n",
    "           \"静夜思。床前明月光，疑是地上霜。举头望明月，低头思故乡。\",\\\n",
    "           \"望庐山瀑布。日照香炉生紫烟，遥看瀑布挂前川。飞流直下三千尺，疑是银河落九天。\",\\\n",
    "           \"赠汪伦。李白乘舟将欲行，忽闻岸上踏歌声。桃花潭水深千尺，不及汪伦送我情。\",\\\n",
    "           \"早发白帝城。朝辞白帝彩云间，千里江陵一日还。两岸猿声啼不住，轻舟已过万重山。\"]\n",
    "\n",
    "#获取诗歌\n",
    "def get_poem_func(task_name, mult_queue):\n",
    "    print(task_name + \"任务启动\")\n",
    "    try:\n",
    "        #获取诗歌并进行发送\n",
    "        for one_poem in rd_poem:\n",
    "            mult_queue.put(one_poem) \n",
    "            print(\"发送内容\")\n",
    "\n",
    "            #等待\n",
    "            time.sleep(10)\n",
    "                \n",
    "    except KeyboardInterrupt:\n",
    "        print(task_name + \"任务被终止\")\n",
    "        \n",
    "#朗读诗歌\n",
    "def read_poem_func(task_name, mult_queue):\n",
    "    print(task_name + \"任务启动\")\n",
    "    try:\n",
    "        while True:\n",
    "            #从队列中诗歌\n",
    "            poem_data = mult_queue.get()\n",
    "            print(\"接收内容\")\n",
    "            cmd = \"ilang \"+poem_data\n",
    "            os.system(cmd)\n",
    "                \n",
    "    except KeyboardInterrupt:\n",
    "        print(task_name + \"任务被终止\")\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    try:\n",
    "        #　定义传递图像队列和传递图像处理结果队列\n",
    "        q_poem = Queue()\n",
    "        \n",
    "        #　采集获取诗集\n",
    "        get_poem  = Process(target=get_poem_func, args=(\"获取诗歌\", q_poem))\n",
    "        read_poem = Process(target=read_poem_func, args=(\"朗读诗歌\", q_poem))\n",
    "              \n",
    "        # 启动任务\n",
    "        get_poem.start()\n",
    "        read_poem.start()\n",
    "        \n",
    "        #等待启动的进程执行结束\n",
    "        get_poem.join()\n",
    "        read_poem.join()\n",
    "\n",
    "    except KeyboardInterrupt:\n",
    "        print(\"任务被终止了\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
